package view;

import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import util.AnalyzeUtil;

import analyze.AnalyzeInfo;

public class ScrollableInfoPanel extends JFrame{
	private static final long serialVersionUID = 1938323584954454653L;

	public ScrollableInfoPanel(ArrayList<AnalyzeInfo> infos, String screenTitle){
		Rectangle usableRect = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
		Dimension dim = new Dimension(usableRect.width, usableRect.height);
		
		JTextArea area = new JTextArea();
		final JScrollPane sp = new JScrollPane(area);
		this.setMinimumSize(new Dimension(300,200));
		StringBuilder text = new StringBuilder("Info from " + screenTitle + " at " + (new Date()) + "\n");
		if(infos!=null && infos.size()>0){
			AnalyzeUtil.sortByOrder(infos);
			for (AnalyzeInfo i : infos) {
				text.append(i.getKey() + ": " + i.getValue()+"\n");
			}
			area.setText(text.toString());
			add(sp);
		}
		area.setEditable(false);
		
		this.pack();
		//center on screen
		this.setSize(this.getWidth() + 15, (dim.height > this.getHeight() ) ? this.getHeight() : dim.height);
		this.setLocation((dim.width-this.getSize().width)/2, (dim.height-this.getSize().height)/2);
		sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
		   public void run() { 
		       sp.getVerticalScrollBar().setValue(0);
		   }
		});
		
		this.setTitle(screenTitle);
		setVisible(true);
	}
}
